package com.flag.liutao.modules.auth.controller;

import cn.hutool.captcha.*;
import com.flag.liutao.common.domain.LocalUser;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.common.enums.AuthEnum;
import com.flag.liutao.common.utils.RedisUtil;
import com.flag.liutao.modules.auth.param.LoginParam;
import com.flag.liutao.modules.auth.service.AuthService;
import com.flag.liutao.modules.user.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.flag.liutao.common.domain.ResponseResult.ok;

@RestController
@Slf4j
@RequiredArgsConstructor
/**
 * @Api -描述当前类型生成帮助文档的信息
 * 属性
 *      -tags:  给当前类型定义别名，可以有多个。定义几个别名，在UI视图中就显示几个控制器访问菜单。
 *      -description：   给当前类型生成帮助文档的描述信息
 */
@Api(tags ="权限管理")
public class AuthController {

    private final AuthService authService;
    private final RedisUtil redisUtil;

    /**
     * 管理员登录
     * @param param
     * @return
     */
    @PostMapping("/admin/login")
    @ApiOperation(value = "管理员登录",notes = "权限认证：管理员登录")
    public ResponseResult<Map<String,String>> adminLogin(@Validated @RequestBody LoginParam param){
        Map<String, String> map = authService.adminLogin(param.getUserName(), param.getPassword(),param.getCode());
        return ResponseResult.ok(map,"登录成功");
    }

    /**
     * 普通用户登录
     * @param param
     * @return
     */
    @PostMapping("/api/login")
    @ApiOperation(value = "普通用户登录",notes = "权限认证：普通用户登录")
    public ResponseResult<Map<String,String>> login(@Validated @RequestBody LoginParam param){
        Map<String, String> map = authService.login(param.getUserName(), param.getPassword(),param.getCode());
        return ResponseResult.ok(map,"登录成功");
    }

    /**
     * 获取图片验证码
     * @return
     */
    @GetMapping("/api/chptcha")
    @ApiOperation(value = "获取验证码",notes = "权限认证：获取验证码")
    public ResponseResult<Map<String,String>> captcha(){
        String code = authService.getCode();
        //通过工具类获取验证码
        LineCaptcha circleCaptcha = CaptchaUtil.createLineCaptcha(200,50,4,50);
        code = circleCaptcha.getCode();
        String imageBase64 = circleCaptcha.getImageBase64();
        log.info("验证码--->"+code);
        authService.setCode(code,10);
        Map map = new HashMap();
        map.put("imageBase64",imageBase64);
        return ResponseResult.ok(map);
    }

    @ApiOperation(value="管理员退出登录",notes = "权限管理：退出登录")
    @PostMapping("/admin/logout")
    @PreAuthorize("hasAuthority('sys_power:login_admin')")
    public ResponseResult<String> logout(HttpServletRequest request){
        String authorization = request.getHeader(AuthEnum.TOKEN_NAME.getValue());
        String token = authorization.split(" ")[1];
        User user = LocalUser.getLocalUser();
        authService.logout(AuthEnum.ADMIN_TOKEN.getValue(),user.getId(),token);
        return ResponseResult.ok("退出成功！");
    }
}
